package com.bootdo.fx.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.UserWithdrawDO;
import com.bootdo.fx.service.UserWithdrawService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */

@Controller
public class UserWithdrawController {
    @Autowired
    private UserWithdrawService userWithdrawService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private IdWorker idWorker;

    @GetMapping("/fx/userWithdraw")
    @RequiresPermissions("fx:userWithdraw:userWithdraw")
    String UserWithdraw() {
        return "fx/userWithdraw/userWithdraw";
    }

    @ResponseBody
    @GetMapping("/fx/userWithdraw/list")
    @RequiresPermissions("fx:userWithdraw:userWithdraw")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<UserWithdrawDO> userWithdrawList = userWithdrawService.list(query);
        int total = userWithdrawService.count(query);
        PageUtils pageUtils = new PageUtils(userWithdrawList, total);
        return pageUtils;
    }

    @GetMapping("/fx/userWithdraw/add")
    @RequiresPermissions("fx:userWithdraw:add")
    String add() {
        return "fx/userWithdraw/add";
    }

    @GetMapping("/fx/userWithdraw/edit/{withdrawId}")
    @RequiresPermissions("fx:userWithdraw:edit")
    String edit(@PathVariable("withdrawId") Long withdrawId, Model model) {
        UserWithdrawDO userWithdraw = userWithdrawService.get(withdrawId);
        model.addAttribute("userWithdraw", userWithdraw);
        return "fx/userWithdraw/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/fx/userWithdraw/save")
    @RequiresPermissions("fx:userWithdraw:add")
    public R save(UserWithdrawDO userWithdraw) {
        if (userWithdrawService.save(userWithdraw) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/fx/userWithdraw/update")
    @RequiresPermissions("fx:userWithdraw:edit")
    public R update(UserWithdrawDO userWithdraw) {
        userWithdrawService.update(userWithdraw);
        return R.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/fx/userWithdraw/verify")
    @RequiresPermissions("fx:userWithdraw:edit")
    public R verify(String withdrawId,String status) {
        UserWithdrawDO u = new UserWithdrawDO();
        u.setWithdrawId(withdrawId);
        u.setWithdrawStatus(Integer.parseInt(status));
        int num = userWithdrawService.update(u);
        if(num > 0){
            return R.ok("审核通过！");
        }else {
            return R.error("审核失败！");
        }
    }


    /**
     * 删除
     */
    @PostMapping("/fx/userWithdraw/remove")
    @ResponseBody
    @RequiresPermissions("fx:userWithdraw:remove")
    public R remove(Long withdrawId) {
        if (userWithdrawService.remove(withdrawId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/fx/userWithdraw/batchRemove")
    @ResponseBody
    @RequiresPermissions("fx:userWithdraw:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] withdrawIds) {
        userWithdrawService.batchRemove(withdrawIds);
        return R.ok();
    }


    /*
    *  佣金提现
    * */
    @GetMapping("/wx/userWithdraw/applyWithdraw")
    @ResponseBody
    public JsonResult applyWithdraw(String withdrawAmount,String withdrawNote,String withdrawChannel,String withdrawAccount,
                                    String withdrawRealName, HttpServletResponse response,String userId, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        //UserInfoDO userInfo = (UserInfoDO) request.getSession().getAttribute("userInfo");
        if (withdrawAmount == null || withdrawRealName == null
                || withdrawChannel == null || withdrawAccount == null
                ) {
            ResultUtil.renderError("输入数据有误或不全！");
        }
        if(userId == null || userId.length() > 0){
            return ResultUtil.renderError();
        }
        //withdrawAmount="99";
        UserWithdrawDO userWithdraw = new UserWithdrawDO();
        userWithdraw.setWithdrawAmount(new BigDecimal(withdrawAmount));
        userWithdraw.setWithdrawNote(withdrawNote);
        userWithdraw.setWithdrawChannel(withdrawChannel);
        userWithdraw.setWithdrawAccount(withdrawAccount);
        userWithdraw.setWithdrawRealName(withdrawRealName);


        userWithdraw.setWithdrawId(String.valueOf(idWorker.nextId()));
        userWithdraw.setWithdrawCreateTime(new Date());
        userWithdraw.setWithdrawStatus(0);
        userWithdraw.setUserId(userId);
        UserInfoDO userInfoDO = userInfoService.get(Long.parseLong(userId));
        if (userWithdraw.getWithdrawRealName() == null || userWithdraw.getWithdrawRealName().length() == 0) {
            userWithdraw.setWithdrawRealName(userInfoDO.getUserName());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("withdrawStatus", "0");
        //List<UserWithdrawDO> list = userWithdrawService.list(map);
        int b;
       /* if (list.size() > 0) {
            b = 0;
        } else {
            b = userWithdrawService.saveWithDraw(userWithdraw,String.valueOf(userInfo.getUserId()));
        }*/
        b = userWithdrawService.saveWithDraw(userWithdraw,String.valueOf(userId));


        if (b > 0) {
            return ResultUtil.renderSuccess("提交成功！");
        } else {
            return ResultUtil.renderError("操作失败！提示：如有待审核提现，则目前无法申请！");
        }
    }

    /*
    *  佣金提现状态查看
    * */
    @RequestMapping("/wx/userWithdraw/listWithdraw")
    @ResponseBody
    public JsonResult listWithdraw(String userId,HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        if(userId == null || userId.length() ==0){
            return ResultUtil.renderError();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        return ResultUtil.renderSuccess(userWithdrawService.list(map));
    }

}
